<!-- 注意：这个文件没有实际用处，单纯只是为了编写页面代码时更加高效，实际页面代码请见：SwaggerWebFilter -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Swagger 登入</title>
</head>
<body>

<style>
    #loginForm {
        margin: 100px auto 0 auto;
        text-align: center;
        font-size: 20px;

    }
    #loginForm>input {
        height: 30px;
        line-height: 30px;
        margin-bottom: 10px;
        width: 400px;
    }
    #loginForm>button {
        height: 40px;
        line-height: 40px;
        margin-bottom: 10px;
        width: 400px;
        margin-left: 60px;
        background-color: cadetblue;
        border: 0;
        font-size: 20px;
        color: white;
        cursor: pointer;
    }
</style>

<form id="loginForm">
    <label>登 入 Swagger</label><br/><br/>
    用户：<input type="text" name="username" id="username" /><br/>
    密码：<input type="password" name="password" id="password" /><br/>
    <button type="button" onclick="doSubmit()">登入</button>
</form>

<script type="application/javascript">

  function doSubmit() {
    let username = document.getElementById("username").value;
    let password = document.getElementById("password").value;
    if (!username || !password) {
      alert("请输入用户名与密码！")
      return false;
    }
    let ajax = new XMLHttpRequest();
    ajax.open('get','http://localhost:10003/swagger/login?username='+username + '&password='+password);
    ajax.send();
    ajax.onreadystatechange = function () {
      if (ajax.readyState===4 &&ajax.status===200) {
        let result = JSON.parse(ajax.responseText);
        if (result.code === 0) {
            let exp = new Date();
            exp.setTime(exp.getTime() + 7200000);
            let expires = exp.toGMTString();
            document.cookie = "swgu=" + username + ";expires="+ expires;
            document.cookie = "swgp=" + password + ";expires="+ expires;
            window.open("http://localhost:10003/doc.html", "_self")
            return;
        }
        alert("登入失败：" + result.msg);
      }
    }
  }

</script>

</body>
</html>